package com.starwish.starwishblog.service;

import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Body;
import com.starwish.starwishblog.entity.Tag;
import com.starwish.starwishblog.mapper.ArticleMapper;
import com.starwish.starwishblog.mapper.BodyMapper;
import com.starwish.starwishblog.mapper.TagMapper;
import com.starwish.starwishblog.mapper.UserMapper;
import com.starwish.starwishblog.vo.ArticleVo;
import com.starwish.starwishblog.vo.HistoryVo;
import com.sun.deploy.uitoolkit.ui.ConsoleHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.util.HtmlUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    BodyMapper bodyMapper;

    @Autowired
    TagMapper tagMapper;

    @Autowired
    UserMapper userMapper;


    public PageInfo<ArticleVo> findArticleByCons(Integer now, Integer size, String aname, String uname) {
        PageHelper.startPage(now,size);
        List<ArticleVo> articleList = articleMapper.selectArticleByCons(aname,uname);
        if(articleList != null){
            for (ArticleVo articleVo : articleList) {
                articleVo.getBody().setContenthtml(HtmlUtils.htmlUnescape(articleVo.getBody().getContenthtml()));
                System.out.println(articleVo.getBody().getContenthtml());
            }
        }
        return new PageInfo<>(articleList);
    }

    public int modifyContent(Integer bid,String html) {
        return articleMapper.updateContent(bid,html);
    }

    public int modifyWeight(Integer id, Integer weight) {
        return articleMapper.updateWeight(id,weight);
    }

    @Transactional
    public int removeByKey(Integer id) {
        //删除内容
        bodyMapper.deleteByPrimaryKey(articleMapper.selectByPrimaryKey(id).getBodyId());
        //删除文章相关的标签
        //先获得文章的标签id
//        List<Tag> tags = tagMapper.selectTagsByArticleId(id);
//        if(tags.len)
//        tagMapper.deleteByArticleId
        return articleMapper.deleteByPrimaryKey(id);
    }

    public PageInfo<ArticleVo> findRecommendArticleByCons(Integer now, Integer size) {
        PageHelper.startPage(now,size);
        List<ArticleVo> recommendArticleList = articleMapper.selectRecommendArticles();
        return new PageInfo<>(recommendArticleList);
    }

    public PageInfo<ArticleVo> findNewArticleByCons(Integer now, Integer size) {
        PageHelper.startPage(now,size);
        List<ArticleVo> recommendArticleList = articleMapper.selectNewArticles();
        return new PageInfo<>(recommendArticleList);
    }

    public PageInfo<ArticleVo> findHotArticleByCons(Integer now, Integer size) {
        PageHelper.startPage(now,size);
        List<ArticleVo> recommendArticleList = articleMapper.selectHotArticles();
        return new PageInfo<>(recommendArticleList);
    }

    public ArticleVo findArticleById(Integer id) {
        ArticleVo articleVo = articleMapper.selectArticleById(id);
        articleVo.getBody().setContenthtml(HtmlUtils.htmlUnescape(articleVo.getBody().getContenthtml()));
        System.out.println(articleVo.getBody().getContenthtml());
        return articleVo;
    }

    @Transactional
    public int publishArticle(ArticleVo articleVo) {
        Body body = articleVo.getBody();
        //向数据库中添加文章主体
        int i = bodyMapper.insert(body);
        articleVo.setBodyId(body.getId());
        //向数据库中添加文章
        int j = articleMapper.insertArticle(articleVo);
        //向数据库中添加标签
        List<Tag> tags = new ArrayList<>();
        List<String> tags1 = articleVo.getTags();
        System.out.println(tags1);
        if(tags1 != null && tags1.size() > 0){
            for (String s : tags1) {
                System.out.println("----->tag"+s);
                Tag temp = new Tag();
                temp.setAuthorId(articleVo.getAuthorId());
                temp.setName(s);
                tags.add(temp);
            }
            tagMapper.insertBatch(tags);
            List<Integer> tids = new ArrayList<>();
            for (Tag tag : tags) {
                System.out.println("=======>"+tag.getId());
                tids.add(tag.getId());
            }
            tagMapper.insertIntoArticleTagBatch(articleVo.getId(),tids);
        }

        return j;
    }


    public List<Tag> findArticleTagsByArticleId(Integer articleId) {
        return tagMapper.selectTagsByArticleId(articleId);
    }

    @Transactional
    public int modifyArticle(ArticleVo article) {
        int i = articleMapper.updateContent(article.getBody().getId(),article.getBody().getContenthtml());
        return articleMapper.update(article);
    }

    public PageInfo<ArticleVo> findArticleByCateId(Integer now, Integer size, Integer cateId) {
        PageHelper.startPage(now,size);
        List<ArticleVo> recommendArticleList = articleMapper.selectArticlesByCateId(cateId);
        return new PageInfo<>(recommendArticleList);
    }

    public PageInfo<ArticleVo> findArticlesByAuthorId(Integer now, Integer size, Integer authorId) {
        PageHelper.startPage(now,size);
        List<ArticleVo> articleList = articleMapper.selectArticlesByAuthorId(authorId);
        return new PageInfo<>(articleList);
    }

    public List<ArticleVo> findHotArticleByAuthorId(Integer authorId) {
        return articleMapper.selectHotArticlesByAuthorId(authorId);
    }

    public List<ArticleVo> findNewArticleByAuthorId(Integer authorId) {
        return articleMapper.selectNewArticlesByAuthorId(authorId);
    }


    public PageInfo<ArticleVo> findCollectionArticleByCons(Integer now, Integer size, Integer authorId, Integer type) {
        PageHelper.startPage(now,size);
        List<ArticleVo> articleList = articleMapper.selectCollectionArticlesByCons(authorId,type);
        return new PageInfo<>(articleList);
    }

    @Transactional
    public Integer cancelCollect(Integer userId, Integer articleId) {
        //文章的收藏量--
        articleMapper.updateArticleCollectionCount(articleId,-1);
        return articleMapper.deleteCollectionByUserIdAndArticleId(userId,articleId);
    }

    @Transactional
    public Integer cancelLike(Integer userId, Integer articleId) {
        articleMapper.updateArticleLikeCount(articleId,-1);
        return articleMapper.deleteLikeByUserIdAndArticleId(userId,articleId);
    }

    public Integer checkIfLike(Integer userId, Integer articleId) {
        return articleMapper.checkIfLike(userId,articleId);

    }

    public Integer checkIfCollect(Integer userId, Integer articleId) {
        return articleMapper.checkIfCollect(userId,articleId);
    }

    @Transactional
    public Integer addToLike(Integer userId, Integer articleId) {
        articleMapper.updateArticleLikeCount(articleId,1);
        return articleMapper.insertArticleLike(userId,articleId);
    }

    @Transactional
    public Integer addToCollection(Integer userId, Integer articleId) {
        articleMapper.updateArticleCollectionCount(articleId,1);
        return articleMapper.insertArticleCollection(userId,articleId);
    }

    public Integer findCountByAuthorId(Integer authorId) {
        return articleMapper.selectArticleCountByAuthorId(authorId);
    }

    @Transactional
    public Integer addViewCountAndViewHistory(Integer userId, Integer articleId) {
        if(userId != -1 && userId != null &&  !"".equals(userId)){
            HistoryVo historyVo = new HistoryVo();
            historyVo.setArticleId(articleId);
            historyVo.setCreateTime(new Date());
            historyVo.setUserId(userId);
            userMapper.insertHistory(historyVo);
        }
        return  articleMapper.updateArticleViewCount(articleId);
    }

    public Integer changeCollectionType(Integer userId, Integer articleId, Integer type) {
        return articleMapper.updateCollectionType(userId, articleId, type);
    }

    public PageInfo<ArticleVo> findArticleByConsAndStatus(Integer now, Integer size, String aname, String uname, Integer status) {
        PageHelper.startPage(now,size);
        List<ArticleVo> articleList = articleMapper.selectArticleByConsAndStatus(aname,uname,status);
        if(articleList != null){
            for (ArticleVo articleVo : articleList) {
                articleVo.getBody().setContenthtml(HtmlUtils.htmlUnescape(articleVo.getBody().getContenthtml()));
                System.out.println(articleVo.getBody().getContenthtml());
            }
        }
        return new PageInfo<>(articleList);
    }

    public Integer changeStatus(Integer articleId, Integer status, String result) {
        return articleMapper.updateArticleStatus(articleId, status, result);
    }

    public PageInfo<ArticleVo> findArticlesByAuthorIdAndStatus(Integer now, Integer size, Integer authorId, Integer status) {
        PageHelper.startPage(now,size);
        List<ArticleVo> articleList = articleMapper.selectArticlesByAuthorIdAndStatus(authorId, status);
        return new PageInfo<>(articleList);
    }

    public List<ArticleVo> findNewArticleByAuthorIdAndStatus(Integer authorId, Integer status) {
        return articleMapper.selectNewArticlesByAuthorIdAndStatus(authorId,status);
    }

    public List<ArticleVo> findHotArticleByAuthorIdAndStatus(Integer authorId, Integer status) {
        return articleMapper.selectHotArticlesByAuthorIdAndStatus(authorId,status);
    }
}
